require.config({
	paths:{
		"jquery":"jquery",
		"common":"xz-common",
		"cookie":"jquery.cookie",
		"fly":"jquery.fly"
	}
})
$pot=1;
require(["common","jquery","cookie"],function(common,$){
	$(function(){
		//是否登录
        if($.cookie("login")){
        	$("#xz-login").text($.cookie("login")+" ，点击重新登录");
        }else{
        	$("#xz-login").text("【登录】");
        }
     //退出
      $("#xz-login").mousedown(function(){
      	$.removeCookie("login",{expires:0,path:"/"});
      	location.href = "../html/login.html";
      })
	})
	$(function(){
	//获取json数据
	var productnum = parseInt($.cookie("product"));

	$.getJSON("../xz.json",function(obj){	
	 $(".xz-topic-main h1").text(obj.main_son1[productnum].main_son1_h4_font);
     $(".xz-product-detail1 span").text(obj.main_son1[productnum].content_salepirce_old);
   	 $(".xz-product-detail2 span").text(obj.main_son1[productnum].content_salepirce_now);
   	 $(".xz-product-detail3 span").text(obj.main_son1[productnum].content_integral);
   	 $("#xz-product-Magnifier img").attr("src","../"+obj.main_son1[productnum].son1_photo);
     $(".xz-bigwarp img").attr("src","../"+obj.main_son1[productnum].son1_photo);
	 $(".xz-topic-main").attr("id",obj.main_son1[productnum].content_id);
     if(obj.main_son1[productnum].son1_photo1){ 
     for(var i = 0 ;i< obj.main_son1[productnum].son1_photo1.length;i++)
      {
      	$(".xz-product-content-smallwarp").append(
      	'<div class="xz-product-small">'
		 	 +'<img src="../'+obj.main_son1[productnum].son1_photo1[i]+'"/>'
		 	 +'</div>')
      }
	}else{
	$(".xz-product-content-smallwarp").append(
		' <div class="xz-product-small"><img src="../img/xz-main-content-warp1-son10.jpg"/></div>'
		 	 + '<div class="xz-product-small">'
		 	 +'<img src="../img/xz-main-content-warp1-son11.jpg"/></div>'
		 	 +'<div class="xz-product-small">'
		 	 +'<img src="../img/xz-main-content-warp1-son12.jpg"/></div>'		 	 
		 	 +'<div class="xz-product-small">'
		 	 +'<img src="../img/xz-main-content-warp1-son13.jpg"/> </div>'
		 )}//为美观加的可删除
	
	})
	})
  	//购物车跳转
    $("#xz-shopcar").mousedown(function(){

    	location.href ="../html/shopcar.html"
    	
    });
    //注册页跳转
    $("#toresgister").mousedown(function(){
    	location.href = "../html/register.html"
    
    });
    //登录页跳转
     $("#xz-login").mousedown(function(){
    	location.href = "../html/login.html"
    	
    });
     //列表页跳转
     $(".xz-listofgoods-one a").mousedown(function(){
     	location.href = "../html/listgoods.html"
     });
	//固定列表效果
	$(function(){
		      
      $(".xz-goods-fixed ul li a").click(function(){
      	 $(".xz-goods-fixed ul li a").css({"color":"#000","border":"none"});
      	 $(this).css({"color":"#ED145B","border-bottom":"3px solid #ED145B"});
      })
      
      $(window).scroll(function(){
       
      	if($(window).scrollTop()>=700){
      		$(".xz-goods-fixed").css({"position":"fixed","top":0})
      	}else{
      		$(".xz-goods-fixed").css({"position":"relative","top":0})
      	}
      })
	})
	
	//放大镜
	$(function(){
	  //加载小图
      //改变放大图片
      $(".xz-product-small img").mouseover(function(){
      
      	$(".xz-bigwarp img").attr("src", $(this).attr("src"));
        $("#xz-product-Magnifier img").attr("src", $(this).attr("src"));
      })

      //实现效果
		$(".xz-bigwarp ").mousemove(function(event){
			$(".xz-filter").css("display","block");
			$("#xz-product-Magnifier").css("display","block")
			var left  = event.pageX - $(".xz-bigwarp").offset().left-$(".xz-bigwarp").scrollLeft()-$(".xz-filter").width()/2;
            var top   = event.pageY - $(".xz-bigwarp").offset().top-$(".xz-bigwarp").scrollTop()-$(".xz-filter").height()/2;
            left=Math.ceil(left);
            top = Math.ceil(top);
            if(left<0){
            	left=0;
            }else if(left>($(".xz-bigwarp").width()-$(".xz-filter").width()))
            {
            	left=$(".xz-bigwarp").width()-$(".xz-filter").width();
            }
            if(top<0){
            	top=0;
            }else if(top>($(".xz-bigwarp").height()-$(".xz-filter").height())){
            	top=$(".xz-bigwarp").height()-$(".xz-filter").height();
            }
           $(".xz-filter").css({"left":left+"px","top":top+"px"});
           $("#xz-product-Magnifier img").css({"left":-left/$(".xz-bigwarp").width()*$("#xz-product-Magnifier img").width(),
           "top":-top/$(".xz-bigwarp").height()*$("#xz-product-Magnifier img").height()})

		})
		$(".xz-bigwarp ").mouseleave(function(){
			$(".xz-filter").css("display","none");
			$("#xz-product-Magnifier").css("display","none");
		})
		
		
	});
  //确定添加物品数量
  $(function(){
  	num=1;
  	$(".xz-buygoods-num").val(num);
    $(".xz-buygoods-leftbut img").mousedown(function(){
  	 if(num <=1){
  	 	num=1
  	 }else{
  	 	num --;  	 	
  	 }
  	 $(".xz-buygoods-num").val(num);
    })

    $(".xz-buygoods-rightbut").mousedown(function(){
    	  	 	num ++;  	 	
  	 $(".xz-buygoods-num").val(num);
    })

  })

  
  //购物车
   $(function(){

   	 $(".xz-content-but").mousedown(function(){
   	 var goods = $.cookie("goods") ? $.cookie("goods"):"";
   	 var id =$(".xz-topic-main").attr("id");
   	  var obj1 = converStrToObj(goods);
   	  if( id in obj1){
   	      obj1[id].num = parseInt(obj1[id].num)+parseInt($(".xz-buygoods-num").val());
   	   	 
   	      
   	   }else{
   	    	obj1[id]={
   	    	name:$(".xz-topic-main h1").text(),
   		  	pirce:$(".xz-product-detail2 span").text(),
   		  	integral:$(".xz-product-detail3 span").text(),
   		  	imgsrc:$("#xz-product-Magnifier img").attr("src"),
   		  	num:parseInt($(".xz-buygoods-num").val())
   	    	}
   	    }
   	    
   	   $.cookie("goods",convertObjTostr(obj1),{expires : 7,path:"/"});
   	  		var goods = converStrToObj($.cookie("goods"));
		var goodsnum =0;
		for(val in goods){
			goodsnum += parseInt(goods[val].num);
		}
		$(".shopcar").text(goodsnum);
   	 })
   	  
   	  
   	  
   	  
   	//字符串转对象
   	function converStrToObj(str){
   		
   		var obj = {};
				if(!str){
					return {};
				}
   		var arr = str.split(":");
   		for(var i = 0 ;i<arr.length;i++){  
   		  arr2=arr[i].split(",");
   		  obj[arr2[0]]={
   		  	name:arr2[1],
   		  	pirce:arr2[2],
            num:arr2[3],
   		  	integral:arr2[4],
   		  	imgsrc:arr2[5]
   		  	
   		  }
   		}
   		return obj;
   	}
   	//对象转字符串
   	function convertObjTostr(obj){
   		var str = "";
   		for(val in obj){
   		if(str){
   			str +=":";
   		 }
   		str +=val+","+obj[val].name+","+obj[val].pirce+","+obj[val].num+","
   		+obj[val].integral+","+obj[val].imgsrc
   		}
   		return str;
   	}
   })
   
   //动态加载数据
 $(function(){
 	 $.getJSON("../xz.json",function(obj){
 	 	 for(var i=6; i< 15;i++){
 	 			$("#hot-top .xz-recommend-warp").append(
 	 				' <div class="xz-recommend-son">'
        	 		+'<a href="#" class="xz-recommend-image" >'
        	 		+	'<img src="../'+obj.main_son1[i].son1_photo+'"/>'
        	 	+	'</a>'
        	 	 +' <p class="xz-recommend-top">'
        	 	   + '	<a href="#">'+obj.main_son1[i].main_son1_h4_font+'</a>'
        	 	  +'  </p>'
        	 	   +' <p class="xz-recommend-bottom">'
        	 	    +	obj.main_son1[i].content_salepirce_now
        	 	    +'<span>'
        	 	    +obj.main_son1[i].content_salepirce_old
        	 	    +'</span>'
        	 	    +'</p>'			
 	 		)};
 	 		  for(var i=16; i< obj.main_son1.length;i++){
 	 			$("#like-you .xz-recommend-warp").append(
 	 				' <div class="xz-recommend-son">'
        	 		+'<a href="#" class="xz-recommend-image" >'
        	 		+	'<img src="../'+obj.main_son1[i].son1_photo+'"/>'
        	 	+	'</a>'
        	 	 +' <p class="xz-recommend-top">'
        	 	   + '	<a href="#">'+obj.main_son1[i].main_son1_h4_font+'</a>'
        	 	  +'  </p>'
        	 	   +' <p class="xz-recommend-bottom">'
        	 	    +	obj.main_son1[i].content_salepirce_now
        	 	    +'<span>'
        	 	    +obj.main_son1[i].content_salepirce_old
        	 	    +'</span>'
        	 	    +'</p>'			
 	 		)};
 	 		 for(var i=2; i< 16;i++){
 	 			$("#user-like .xz-recommend-warp").append(
 	 				' <div class="xz-recommend-son">'
        	 		+'<a href="#" class="xz-recommend-image" >'
        	 		+	'<img src="../'+obj.main_son1[i].son1_photo+'"/>'
        	 	+	'</a>'
        	 	 +' <p class="xz-recommend-top">'
        	 	   + '	<a href="#">'+obj.main_son1[i].main_son1_h4_font+'</a>'
        	 	  +'  </p>'
        	 	   +' <p class="xz-recommend-bottom">'
        	 	    +	obj.main_son1[i].content_salepirce_now
        	 	    +'<span>'
        	 	    +obj.main_son1[i].content_salepirce_old
        	 	    +'</span>'
        	 	    +'</p>'			
 	 		)}
 	 	
 	 })
 })
   
   
})
